package com.test.ssm.controller;

import com.test.ssm.pojo.Student;
import com.test.ssm.pojo.Teacher;
import com.test.ssm.service.StudentService;
import com.test.ssm.service.TeacherService;
import com.test.ssm.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @author 徒有琴
 */
@Controller
public class UserController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;

    @RequestMapping("gologin.html")
    public String login() {
        return "login";
    }

    @RequestMapping("login.html")
    public String login(String email, String password, Integer type, Model model, HttpSession session) {
        //判断email和password参数为空，略
        //常量全部提取出来
        String page="login";
        Object user = null;
        if (type == Constants.USER_TYPE_TEACHER) {//1是老师，2是学生
            user = teacherService.doTeacherLogin(email, password);
            page = "teacher/index";
        } else if (type == Constants.USER_TYPE_STUDENT) {
            user = studentService.doStudentLogin(email, password);
            page = "student/index";
        }
        if (user == null) {
            model.addAttribute("message", "用户名或密码错误");
            return "login";
        } else {
            session.setAttribute(Constants.USER_TYPE, type);
            session.setAttribute(Constants.SESSION_USER, user);
        }
        //学生的略
        return page;
    }

    @RequestMapping("logout.html")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }
}
